package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Filetype;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FiletypeDAO")
@Transactional
public class FiletypeDAOImpl extends AbstractJpaDao<Filetype> implements FiletypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Filetype.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FiletypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Filetype findFiletypeById(Integer id) throws DataAccessException {

		return findFiletypeById(id, -1, -1);
	}

	@Transactional
	public Filetype findFiletypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFiletypeById", startResult, maxRows, id);
			return (Filetype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Filetype> findFiletypeByName(String name) throws DataAccessException {

		return findFiletypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filetype> findFiletypeByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFiletypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Filetype>(query.getResultList());
	}

	@Transactional
	public Set<Filetype> findFiletypeByNameContaining(String name) throws DataAccessException {

		return findFiletypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filetype> findFiletypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFiletypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Filetype>(query.getResultList());
	}

	@Transactional
	public Set<Filetype> findFiletypeByDescription(String description) throws DataAccessException {

		return findFiletypeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filetype> findFiletypeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFiletypeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Filetype>(query.getResultList());
	}

	@Transactional
	public Set<Filetype> findFiletypeByDescriptionContaining(String description) throws DataAccessException {

		return findFiletypeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filetype> findFiletypeByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFiletypeByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Filetype>(query.getResultList());
	}

	@Transactional
	public Filetype findFiletypeByPrimaryKey(Integer id) throws DataAccessException {
		return findFiletypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Filetype findFiletypeByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFiletypeByPrimaryKey", startResult, maxRows, id);
			return (Filetype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Filetype> findFiletypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFiletypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Filetype> findAllFiletypes() throws DataAccessException {

		return findAllFiletypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filetype> findAllFiletypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFiletypes", startResult, maxRows);
		return new LinkedHashSet<Filetype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Filetype> findFiletypeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Filetype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Filetype> findFiletypesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Filetype.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Filetype> filetypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return filetypes;
	}

	@Transactional
	public Filetype findFiletypeByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findFiletypeByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Filetype findFiletypeByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFiletypeByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Filetype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Filetype entity) {
		return true;
	}
}
